package com.fang.transfer.file.client.thread;

import com.fang.transfer.file.client.AppContext;
import com.fang.transfer.file.client.connection.UdpUtil;
import com.fang.transfer.file.client.transfer.UdpService;
import com.fang.transfer.file.common.code.ClientSendCode;
import com.fang.transfer.file.common.model.TransferFileRequest;

/**
 * 发送udp请求到服务器
 */
public class SendUDPAddrService implements Runnable {

    private TransferFileRequest request;
    private UdpService udpService;

    public SendUDPAddrService(TransferFileRequest request) {
        this.request = request;
        this.udpService = AppContext.udpServiceMap.get(request.getId());
        this.request.setStatus(2);
    }

    @Override
    public void run() {
        try {
            Thread.currentThread().setName("SendUDPAddrService");
            doRun();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void doRun() throws Exception {
        AppContext.udpServiceMap.get(request.getId());
        while (request.getStatus() == 2) {
            System.out.println("发送UPD同步地址信息");
            udpService.sendServiceInfo(ClientSendCode.UPD_ADDR_UPDATE, request);
            Thread.sleep(1000L);
        }
        System.out.println("地址同步结束");
    }
}
